package com.xbdun.web.validatebmp.upload.service.manage;

import java.io.File;
import java.util.List;

import javax.validation.ValidationException;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbdun.ValidateWebApplication;
import com.xbdun.dao.validatebmp.entity.EcUploadFileDetail;
import com.xbdun.facade.validatebmp.exceptions.ValidateBizException;
import com.xbdun.web.validatebmp.upload.core.service.UploadManageOpenService;

import lombok.extern.slf4j.Slf4j;
/**
 * 文件上传业务接口实现类
 */
@Service
@Slf4j
public class UploadManageOpenServiceImpl implements UploadManageOpenService{
	private String delExt="_del_x7_";//删除文件显示前缀
	
	@Autowired
	private UploadFileManager uploadFileManager;
	
	@Transactional(rollbackFor=Exception.class)
	public int delete(long id) throws ValidateBizException {
		try {
			List<EcUploadFileDetail> list = uploadFileManager.queryFileDetails(id);
			if(list==null||list.size()==0){
				return 0;
			}
			uploadFileManager.delFileInfo(id);
			for (EcUploadFileDetail ecUploadFileDetail : list) {
				String fileurl = ecUploadFileDetail.getFileUrl();
				String filepath = fileurl.substring(0,fileurl.lastIndexOf(File.separator));
				String filename = fileurl.substring(fileurl.lastIndexOf(File.separator),fileurl.length()).replace(File.separator, "");
				try {
					FileUtils.moveFile(new File(ValidateWebApplication.getSaveFolder()+fileurl), new File(ValidateWebApplication.getSaveFolder()+filepath+File.separator+delExt+filename));
				} catch (Exception e) {
					log.error("File delete err:'"+ecUploadFileDetail.getFileUrl()+"'",e);
					throw new ValidationException(e);
				}
			}
			return list.size();
		} catch (Exception e) {
			log.error("delete err:",e);
			throw new ValidationException(e);
		}	
	}
	
	public int delete(long... ids) throws ValidationException {
		int size = 0;
		for (long id : ids) {
			try{
				size+=delete(id);
			}catch(Exception e){
				log.error("File delete err: uploadId="+id ,e);
				throw new ValidationException(e);
			}
		}
		return size;
	}
	
	public int delete(String ids) throws ValidateBizException {
		int size = 0;
		for (String id : ids.split(",")) {
			long uploadId = Long.parseLong(id);
			try{
				size+=delete(uploadId);
			}catch(Exception e){
				log.error("File delete err: uploadId="+id ,e);
				throw new ValidationException(e);
			}
		}
		return size;
	}

	public long getTotalSize() throws ValidateBizException {
		try{
			return uploadFileManager.getTotalSize();
		}catch(Exception e){
			log.error("getTotalSize err:",e);
			throw new ValidationException(e);
		}
	}

	public long getTotalQuantity() throws ValidateBizException {
		try{
			return uploadFileManager.getTotalQuantity();
		}catch(Exception e){
			log.error("getTotalQuantity err:",e);
			throw new ValidationException(e);
		}
	}
	
	public long getTotalDeletedQuantity() throws ValidateBizException {
		try{
			return uploadFileManager.getTotalDeletedQuantity();
		}catch(Exception e){
			throw new ValidationException(e);
		}
	}
	
}
